package com.koocloud.electroplatemanage.controller;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.pojo.OrderNumberVo;
import com.koocloud.electroplatemanage.service.OrderNumberService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;


/**
 * @program: electroplatemanage
 * @description: 基础信息源订单号控制类
 * @author: zww
 * @create: 2020-11-17 10:43
 */
@RestController
@RequestMapping("/ordernumber")
public class OrderNumberController {
    static Logger log = Logger.getLogger("ordernumber");
    @Autowired
    OrderNumberService orderNumberService;

    /**
     * @Description:通过pk查询图号表一条记录
     * @Param: [pk]
     * @return: com.koocloud.electroplatemanage.common.ResponseTemplate
     * @Author: zww
     * @date: 2020-11-17
     */
    @RequestMapping(value = "/selectByPk", method = RequestMethod.POST, name = "主键查询")
    public ResponseTemplate selectByPk(String pkOrderNumber) {
        //通过pk查询基础信息源订单表的一条记录
        return orderNumberService.selectByPk(pkOrderNumber);
    }

    /**
     * @Description: 查询所有记录 分页  订单号实体
     * @Param: [page,limit,orderNumberVo]
     * @return: com.koocloud.electroplatemanage.common.ResponseTemplate
     * @Author: zww
     * @date: 2020-11-17
     */
    @RequestMapping(value = "/selectAllData", method = RequestMethod.POST, name = "全表查询")
    public ResponseTemplate selectAllData(String page, String limit, OrderNumberVo orderNumberVo) {
        //分页参数为空则赋值0
            if (!StringUtils.isNotEmpty(page)) {
                page = "0";
            }
            if (!StringUtils.isNotEmpty(limit)) {
                limit = "0";
            }
        //查询所有记录 分页
        return orderNumberService.selectAllData(page,limit,orderNumberVo);
    }

    /**
     *@Description: 生成订单号
     *@Param:
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020-11-17
     */
    @RequestMapping(value = "/generateOrderNumber", method = RequestMethod.POST, name = "生成订单号")
    public ResponseTemplate generateOrderNumber() {
        //生成订单号
        return orderNumberService.generateOrderNumber();

    }
    /**
     *@Description:插入一条记录
     *@Param: [orderNumberVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020-11-17
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, name = "新增一条数据")
    public ResponseTemplate insert(@RequestBody OrderNumberVo orderNumberVo) {
        //插入一条记录
        return orderNumberService.insert(orderNumberVo);
    }
    /**
     *@Description:逻辑删除一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020-11-17
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, name = "逻辑删除一条记录")
    public ResponseTemplate delete(@RequestParam(name="orderNumber") String orderNumber,@RequestParam(name="deleteFlag") String deleteFlag) {
        //逻辑删除一条记录
        return orderNumberService.delete(orderNumber,deleteFlag);
    }
    /**
     *@Description:修改一条记录
     *@Param: [orderNumberVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date:2020-11-17
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, name = "修改一条记录")
    public ResponseTemplate update(@RequestBody OrderNumberVo orderNumberVo) {
        //修改一条记录
        return orderNumberService.update(orderNumberVo);
    }
    /**
     *@Description:excel导入
     *@Param: [request, files]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020/11/17
     */
    @RequestMapping(value = "/getExcelImport", method = RequestMethod.POST, name = "excel数据导入")
    public ResponseTemplate getExcelImport(@RequestParam("file") MultipartFile file){
        String filename = file.getOriginalFilename();
        String path = System.getProperty("user.dir");
        try {
            BufferedOutputStream stream = null;
            path = System.getProperty("user.dir");
            byte[] bytes = file.getBytes();
            stream = new BufferedOutputStream(new FileOutputStream(new File(path+ File.separator + filename)));
            stream.write(bytes);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String files = path + File.separator +filename;
        // excel数据导入
        return orderNumberService.getExcelImport(files);
    }
}